package com.lxs.legou.item.service.impl;

import com.lxs.legou.core.service.impl.CrudServiceImpl;
import com.lxs.legou.item.dao.BrandDao;
import com.lxs.legou.item.po.Brand;
import com.lxs.legou.item.po.Category;
import com.lxs.legou.item.service.IBrandService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BrandServiceImpl extends CrudServiceImpl<Brand> implements IBrandService {

    /**
     * 保存品牌
     * @param entity
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean saveOrUpdate(Brand entity) {

        boolean result = super.saveOrUpdate(entity);

        //删除商品和分类的关联
        ((BrandDao) getBaseMapper()).deleteCategoryByBrand(entity.getId());

        //添加商品和分类的关联
        Long[] categoryIds = entity.getCategoryIds();
        if (null != categoryIds){
            for (Long categoryId : categoryIds) {
                ((BrandDao) getBaseMapper()).insertCategoryAndBrand(categoryId,entity.getId());
            }
        }

        return result;
    }

    /**
     * 根据商品id查询分类(商品可以对应多个分类，就可能有多个分类id)
     * @param id
     * @return
     */
    @Override
    public List<Category> selectCategoryByBrand(Long id) {
        return ((BrandDao)getBaseMapper()).selectCategoryByBrand(id);
    }

}
